//3106.满足距离约束且字典序最小的字符串
//https://leetcode.cn/problems/lexicographically-smallest-string-after-operations-with-constraint
class Solution {
public:
    string getSmallestString(string s, int k) {
        int n = s.size();
        for(int i = 0;i<n && k;++i)
        {
            //比较当前字母和a或者z的差值
            //例如 a --- g --- z 显然g与a近一点 消耗的操作次数少一点
            //因为是左避右开区间 所以右的z区间需要+1
            int diff = min(s[i]-'a','z'-s[i]+1);
            //能为a就为a 不能就为z 或者能够调整的最大次数
            if(diff <= k)
            {
                s[i]='a';
                k -= diff;
            }
            else 
            {
                s[i] -= k;
                k = 0;
            }
        }
        return s;
    }
};